package com.fram.controller.admin;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.admin.volume.request.VolumePaperRquest;
import com.fram.bean.admin.volume.request.VolumeUpdateRequest;
import com.fram.bean.admin.volume.response.VolumeListResponse;
import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseAdminController;
import com.fram.entity.VolumeAnswer;
import com.fram.entity.VolumeOption;
import com.fram.entity.VolumePaper;
import com.fram.entity.VolumeQuestion;
import com.fram.service.admin.VolumeAddService;

/**
 * @作者：大晓刚 @时间 2020年5月19日 @dascription: ->后台调查问卷
 */
@RestController
@RequestMapping("/admin/{version}/auth/volume")
public class VolumeAddController extends BaseAdminController {
	/** 日志 **/
	private static Logger LOG = LoggerFactory.getLogger(VolumeAddController.class);

	@Autowired
	private VolumeAddService adminVolumeService;

	/** 列表 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	private ResponseMsg list(@RequestBody PageBean<VolumeQuestion> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(adminVolumeService.listTopic(request));
		} catch (ProException e) {
			LOG.error("-问题列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-问题列表->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 问题详情 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/details", method = RequestMethod.GET)
	private ResponseMsg details(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(adminVolumeService.details(id));
		} catch (ProException e) {
			LOG.error("-问题详情->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-问题详情->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 后台修改问题 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	private ResponseMsg update(@RequestBody VolumeUpdateRequest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			adminVolumeService.update(request);
		} catch (ProException e) {
			LOG.error("-问题  选择 修改出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-问题  选择 修改异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	/** 后台-修改选择题 **/
//	@ApiVersion(1.0)
//	@RequestMapping(value = "/update/option", method = RequestMethod.POST)
//	private ResponseMsg updateOption(@RequestBody VolumeOption request) {
//		ResponseMsg msg = ResponseMsg.returnBeanBuild();
//		try {
//			adminVolumeService.updateOption(request);
//		} catch (ProException e) {
//			LOG.error("-选择题修改->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
//			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
//		} catch (Exception e) {
//			LOG.error("-选择题修改->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
//			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
//		}
//		return msg;
//	}

	/** 后台-删选择题 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/delete/option", method = RequestMethod.GET)
	private ResponseMsg deleteOption(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			adminVolumeService.deleteOption(id);
		} catch (ProException e) {
			LOG.error("-删除选择题->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-删除选择题->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 添加问题 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/add/question", method = RequestMethod.POST)
	private ResponseMsg addQuestion(@RequestBody VolumeListResponse request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			adminVolumeService.addQuestion(request);
		} catch (ProException e) {
			LOG.error("-增加问题->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-增加问题->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 试卷添加问题 (往试卷添加问题 多个问题) **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/add/paper", method = RequestMethod.POST)
	private ResponseMsg addPaper(@RequestBody VolumePaperRquest request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			adminVolumeService.addPaper(request);
		} catch (ProException e) {
			LOG.error("-后台-增加试卷题->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--后台-增加试卷题-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 一个问题增加多个选择题（abcde） **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/add/option", method = RequestMethod.POST)	
	private ResponseMsg addOption(@RequestBody List<VolumeOption> volumeOption) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			adminVolumeService.addOption(volumeOption);
		} catch (ProException e) {
			LOG.error("-增加问题 的选择题->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-增加问题 的选择题->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 版本列表 连表题 一对多查询 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/paper/list", method = RequestMethod.POST)
	private ResponseMsg listPaper(@RequestBody PageBean<VolumePaper> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(adminVolumeService.listPaper(request));
		} catch (ProException e) {
			LOG.error("-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 下拉版本 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/paper/dropdown", method = RequestMethod.POST)
	private ResponseMsg dropDown() {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(adminVolumeService.downPaper());
		} catch (ProException e) {
			LOG.error("-->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	/**list name **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/list/user",method =RequestMethod.POST)
	private ResponseMsg listBy(@RequestBody PageBean<VolumeAnswer> request) {
		ResponseMsg msg=ResponseMsg.returnBeanBuild();
		try {
			msg.setData(adminVolumeService.listByName(request));
		} catch (ProException e) {
			LOG.error("-解答完用户列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-解答完用户列表->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	/** 针对用户 id 查询 回答完的所有 问答 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/answer/details", method = RequestMethod.POST)
	private ResponseMsg listAnswerId(@RequestBody VolumeAnswer request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(adminVolumeService.listByUserId(request));
		} catch (ProException e) {
			LOG.error("-解答完用户列表-详情所有 题->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-解答完用户列表-详情所有 题-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	/****/
	@ApiVersion(1.0)
	@RequestMapping("paper/delete")
	private ResponseMsg paperDelete(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			adminVolumeService.deletePaper(id);
		} catch (ProException e) {
			LOG.error("-删除版本里的问题 出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-删除版本里的问题 异常-->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
